package com.revenco.bi.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.revenco.bi.dao.ChartSqlDao;
import com.revenco.bi.dao.TnDataDrillRelaDao;
import com.revenco.bi.dao.ViewEditChartDao;
import com.revenco.bi.model.TnChartBaseinfo;
import com.revenco.bi.model.TnChartDataitems;
import com.revenco.bi.model.TnChartParams;
import com.revenco.bi.model.TnDataDrillRela;
import com.revenco.bi.service.TnDataDrillRelaService;

@Service
public class TnDataDrillRelaServiceImpl implements TnDataDrillRelaService {
	@Autowired
	private TnDataDrillRelaDao drillValueDao;
	@Autowired
	ViewEditChartDao vecDao;
	@Autowired
	ChartSqlDao chartSqlDao;
	@Autowired
	ViewEditChartServiceImpl viewEditChartService;

	public List<TnDataDrillRela> getDrillValue(String columnId, String drillPath) {
		List<TnDataDrillRela> list = null;
		if (drillPath.equals("")) {
			list = drillValueDao.getDrillValueById(columnId);
		} else {
			TnDataDrillRela tnDataDRrillRela = drillValueDao.getDataDrillRela(
					columnId, drillPath);
			list = new ArrayList<TnDataDrillRela>();
			list.add(tnDataDRrillRela);
		}

		return list;
	}

	/**
	 * 下钻的设置，把是否可下钻，下钻路径以及条件返回到前端
	 */
	public String chartDrillCheck(String chartId, String level, String dirllType) {
		Map<String, String> drillMap = new HashMap<String, String>();
		boolean drillStatus = false;
		try {
			TnChartBaseinfo tcb = vecDao.getChartInfo(chartId);
			String drillPath = tcb.getDrillPath();
			String tableName = tcb.getxFieldTableName();
			TnDataDrillRela tnDataDrillRela = getDataDrillRela(
					tcb.getColumnId(), drillPath);
			if (tnDataDrillRela != null) {// 判断是否有钻取路径
				String drillNamePath = tnDataDrillRela.getNamePath();
				String[] drillPathArray = splitStr(drillPath, ">");
				String[] drillNamePathArray = splitStr(drillNamePath, ">");
				int drillLevel = Integer.parseInt(level);
				if (drillLevel < drillPathArray.length) {// 判断是否复合钻取条件
					drillStatus = true;
					String columnId = drillPathArray[drillLevel];
					String namePath = drillNamePathArray[drillLevel];
					String conditionId = "";
					if ("up".equals(dirllType)) {// 上钻
						conditionId = drillNamePathArray[drillLevel - 1];
					} else {
						conditionId = drillNamePathArray[drillLevel - 1];
					}
					drillMap.put("columnId", columnId);
					drillMap.put("namePath", namePath);
					drillMap.put("conditionId", conditionId);
					drillMap.put("tableName", tableName);
				}
			}
		} catch (Exception e) {

		}
		drillMap.put("drillStatus", drillStatus + "");
		return Json.toJson(drillMap);
	}

	/**
	 * 图形钻取
	 */
	public String chartDrillDown(Model model, HttpServletResponse response,
			String chartId, String columnId, String namePath,
			String conditionId, String idValue, String tableName, String params, String mulRelationData) {
		String viewUrl = "";
		viewUrl = processChartInfo(model, response, chartId, columnId,
				namePath, conditionId, idValue, tableName, params, mulRelationData);
		return viewUrl;
	}

	private String processChartInfo(Model model, HttpServletResponse response,
			String chartId, String columnId, String namePath,
			String conditionId, String idValue, String tableName, String params, String mulRelationData) {
		String vielUrlPath = "";
		try {
			TnChartBaseinfo tcb = vecDao.getChartInfo(chartId);
			List<TnChartDataitems> tcDataList = vecDao.getChartDatas(chartId);
			List<TnChartParams> tcParamsList = vecDao.getChartParams(chartId);
			tcb.setColumnId(columnId);
			tcb.setxFieldCd(namePath);
			TnChartParams tnChartParams = new TnChartParams();
			tnChartParams.setChartId(chartId);
			tnChartParams.setOpType("=");
			tnChartParams.setParamCd(conditionId);
			tnChartParams.setDefaultValue(idValue);
			tnChartParams.setParamType("valuePara");
			tnChartParams.setParamTextvalueFromTableNam(tableName);
			tcParamsList.add(tnChartParams);
			String setSql = viewEditChartService.getPieceSql(tcb, tcDataList,
					tcParamsList, mulRelationData);
			tcb.setChartDataSql(setSql);
			vielUrlPath = viewEditChartService.viewAndEditChart(model,
					response, tcb, tcDataList, tcParamsList, params);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return vielUrlPath;

	}

	/**
	 * 分割
	 */
	public String[] splitStr(String str, String regx) {
		String[] splitStr = str.split(regx);
		for (int i = 0; i < splitStr.length; i++) {
			String[] removeAlias = splitStr[i].split("[.]");
			splitStr[i] = removeAlias[removeAlias.length - 1];

		}
		return splitStr;
	}

	public TnDataDrillRela getDataDrillRela(String columnId, String drillPath) {
		return this.drillValueDao.getDataDrillRela(columnId, drillPath);
	}

}
